
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/utilities/codegen.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:11 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Codegen &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="codegen.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Decorator" href="decorator.html" />
    <link rel="prev" title="Autowrap Module" href="autowrap.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="decorator.html" title="Decorator"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="autowrap.html" title="Autowrap Module"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Utilities</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Codegen</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="codegen">
<span id="codegen-api"></span><h1>Codegen<a class="headerlink" href="#codegen" title="Permalink to this headline">¶</a></h1>
<p>This module provides functionality to generate directly compilable code from
SymPy expressions.  The <code class="docutils literal notranslate"><span class="pre">codegen</span></code> function is the user interface to the code
generation functionality in SymPy.  Some details of the implementation is given
below for advanced users that may want to use the framework directly.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">codegen</span></code> callable is not in the sympy namespace automatically,
to use it you must first execute</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">codegen</span>
</pre></div>
</div>
</div>
<section id="implementation-details">
<h2>Implementation Details<a class="headerlink" href="#implementation-details" title="Permalink to this headline">¶</a></h2>
<p>Here we present the most important pieces of the internal structure, as
advanced users may want to use it directly, for instance by subclassing a code
generator for a specialized application.  <strong>It is very likely that you would
prefer to use the codegen() function documented above.</strong></p>
<p>Basic assumptions:</p>
<ul class="simple">
<li><p>A generic Routine data structure describes the routine that must be translated
into C/Fortran/… code. This data structure covers all features present in
one or more of the supported languages.</p></li>
<li><p>Descendants from the CodeGen class transform multiple Routine instances into
compilable code. Each derived class translates into a specific language.</p></li>
<li><p>In many cases, one wants a simple workflow. The friendly functions in the last
part are a simple api on top of the Routine/CodeGen stuff. They are easier to
use, but are less powerful.</p></li>
</ul>
</section>
<section id="routine">
<h2>Routine<a class="headerlink" href="#routine" title="Permalink to this headline">¶</a></h2>
<p>The Routine class is a very important piece of the codegen module. Viewing the
codegen utility as a translator of mathematical expressions into a set of
statements in a programming language, the Routine instances are responsible for
extracting and storing information about how the math can be encapsulated in a
function call.  Thus, it is the Routine constructor that decides what arguments
the routine will need and if there should be a return value.</p>
</section>
<section id="module-sympy.utilities.codegen">
<span id="api-reference"></span><h2>API Reference<a class="headerlink" href="#module-sympy.utilities.codegen" title="Permalink to this headline">¶</a></h2>
<p>module for generating C, C++, Fortran77, Fortran90, Julia, Rust
and Octave/Matlab routines that evaluate sympy expressions.
This module is work in progress.
Only the milestones with a ‘+’ character in the list below have been completed.</p>
<p>— How is sympy.utilities.codegen different from sympy.printing.ccode? —</p>
<p>We considered the idea to extend the printing routines for sympy functions in
such a way that it prints complete compilable code, but this leads to a few
unsurmountable issues that can only be tackled with dedicated code generator:</p>
<ul class="simple">
<li><p>For C, one needs both a code and a header file, while the printing routines
generate just one string. This code generator can be extended to support
.pyf files for f2py.</p></li>
<li><p>SymPy functions are not concerned with programming-technical issues, such
as input, output and input-output arguments. Other examples are contiguous
or non-contiguous arrays, including headers of other libraries such as gsl
or others.</p></li>
<li><p>It is highly interesting to evaluate several sympy functions in one C
routine, eventually sharing common intermediate results with the help
of the cse routine. This is more than just printing.</p></li>
<li><p>From the programming perspective, expressions with constants should be
evaluated in the code generator as much as possible. This is different
for printing.</p></li>
</ul>
<p>— Basic assumptions —</p>
<ul class="simple">
<li><p>A generic Routine data structure describes the routine that must be
translated into C/Fortran/… code. This data structure covers all
features present in one or more of the supported languages.</p></li>
<li><p>Descendants from the CodeGen class transform multiple Routine instances
into compilable code. Each derived class translates into a specific
language.</p></li>
<li><p>In many cases, one wants a simple workflow. The friendly functions in the
last part are a simple api on top of the Routine/CodeGen stuff. They are
easier to use, but are less powerful.</p></li>
</ul>
<p>— Milestones —</p>
<ul class="simple">
<li><p>First working version with scalar input arguments, generating C code,
tests</p></li>
<li><p>Friendly functions that are easier to use than the rigorous
Routine/CodeGen workflow.</p></li>
<li><p>Integer and Real numbers as input and output</p></li>
<li><p>Output arguments</p></li>
<li><p>InputOutput arguments</p></li>
<li><p>Sort input/output arguments properly</p></li>
<li><p>Contiguous array arguments (numpy matrices)</p></li>
<li><p>Also generate .pyf code for f2py (in autowrap module)</p></li>
<li><p>Isolate constants and evaluate them beforehand in double precision</p></li>
<li><p>Fortran 90</p></li>
<li><p>Octave/Matlab</p></li>
</ul>
<ul class="simple">
<li><p>Common Subexpression Elimination</p></li>
<li><p>User defined comments in the generated code</p></li>
<li><p>Optional extra include lines for libraries/objects that can eval special
functions</p></li>
<li><p>Test other C compilers and libraries: gcc, tcc, libtcc, gcc+gsl, …</p></li>
<li><p>Contiguous array arguments (sympy matrices)</p></li>
<li><p>Non-contiguous array arguments (sympy matrices)</p></li>
<li><p>ccode must raise an error when it encounters something that can not be
translated into c. ccode(integrate(sin(x)/x, x)) does not make sense.</p></li>
<li><p>Complex numbers as input and output</p></li>
<li><p>A default complex datatype</p></li>
<li><p>Include extra information in the header: date, user, hostname, sha1
hash, …</p></li>
<li><p>Fortran 77</p></li>
<li><p>C++</p></li>
<li><p>Python</p></li>
<li><p>Julia</p></li>
<li><p>Rust</p></li>
<li><p>…</p></li>
</ul>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.Argument">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">Argument</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">datatype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dimensions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L363-L369"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.Argument" title="Permalink to this definition">¶</a></dt>
<dd><p>An abstract Argument data structure: a name and a data type.</p>
<p>This structure is refined in the descendants below.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CCodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">CCodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">preprocessor_statements</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cse</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L865-L1083"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CCodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generator for C code.</p>
<p>The .write() method inherited from CodeGen will output a code file and
an interface file, &lt;prefix&gt;.c and &lt;prefix&gt;.h respectively.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CCodeGen.dump_c">
<span class="sig-name descname"><span class="pre">dump_c</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1026-L1027"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CCodeGen.dump_c" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CCodeGen.dump_h">
<span class="sig-name descname"><span class="pre">dump_h</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1031-L1078"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CCodeGen.dump_h" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes the C header file.</p>
<p>This file contains all the function declarations.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to construct the include guards.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CCodeGen.get_prototype">
<span class="sig-name descname"><span class="pre">get_prototype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routine</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L897-L921"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CCodeGen.get_prototype" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string for the function prototype of the routine.</p>
<p>If the routine has multiple result objects, an CodeGenError is
raised.</p>
<p>See: <a class="reference external" href="https://en.wikipedia.org/wiki/Function_prototype">https://en.wikipedia.org/wiki/Function_prototype</a></p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">CodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cse</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L538-L844"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Abstract class for the code generators.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CodeGen.dump_code">
<span class="sig-name descname"><span class="pre">dump_code</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L793-L844"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CodeGen.dump_code" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CodeGen.routine">
<span class="sig-name descname"><span class="pre">routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L576-L748"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CodeGen.routine" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an Routine object that is appropriate for this language.</p>
<p>This implementation is appropriate for at least C/Fortran.  Subclasses
can override this if necessary.</p>
<p>Here, we assume at most one return value (the l-value) which must be
scalar.  Additional outputs are OutputArguments (e.g., pointers on
right-hand-side or pass-by-reference).  Matrices are always returned
via OutputArguments.  If <code class="docutils literal notranslate"><span class="pre">argument_sequence</span></code> is None, arguments will
be ordered alphabetically, but with all InputArguments first, and then
OutputArgument and InOutArguments.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.CodeGen.write">
<span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">to_files</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L750-L791"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.CodeGen.write" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes all the source code files for the given routines.</p>
<p>The generated source is returned as a list of (filename, contents)
tuples, or is written to files (see below).  Each filename consists
of the given prefix, appended with an appropriate extension.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances to be written</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The prefix for the output files</p>
</div></blockquote>
<p><strong>to_files</strong> : bool, optional</p>
<blockquote>
<div><p>When True, the output is written to files.  Otherwise, a list
of (filename, contents) tuples is returned.  [default: False]</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file. [default: True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files. [default: True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.DataType">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">DataType</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pyname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">jlname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">octname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rsname</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L237-L245"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.DataType" title="Permalink to this definition">¶</a></dt>
<dd><p>Holds strings for a certain datatype in different languages.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.FCodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">FCodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1091-L1303"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.FCodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generator for Fortran 95 code</p>
<p>The .write() method inherited from CodeGen will output a code file and
an interface file, &lt;prefix&gt;.f90 and &lt;prefix&gt;.h respectively.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.FCodeGen.dump_f95">
<span class="sig-name descname"><span class="pre">dump_f95</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1251-L1259"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.FCodeGen.dump_f95" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.FCodeGen.dump_h">
<span class="sig-name descname"><span class="pre">dump_h</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1263-L1298"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.FCodeGen.dump_h" title="Permalink to this definition">¶</a></dt>
<dd><p>Writes the interface to a header file.</p>
<p>This file contains all the function declarations.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.FCodeGen.get_interface">
<span class="sig-name descname"><span class="pre">get_interface</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routine</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1201-L1217"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.FCodeGen.get_interface" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string for the function interface.</p>
<p>The routine should have a single result object, which can be None.
If the routine has multiple result objects, a CodeGenError is
raised.</p>
<p>See: <a class="reference external" href="https://en.wikipedia.org/wiki/Function_prototype">https://en.wikipedia.org/wiki/Function_prototype</a></p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.JuliaCodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">JuliaCodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1306-L1504"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.JuliaCodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generator for Julia code.</p>
<p>The .write() method inherited from CodeGen will output a code file
&lt;prefix&gt;.jl.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.JuliaCodeGen.dump_jl">
<span class="sig-name descname"><span class="pre">dump_jl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1496-L1497"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.JuliaCodeGen.dump_jl" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.JuliaCodeGen.routine">
<span class="sig-name descname"><span class="pre">routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1320-L1409"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.JuliaCodeGen.routine" title="Permalink to this definition">¶</a></dt>
<dd><p>Specialized Routine creation for Julia.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.OctaveCodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">OctaveCodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1507-L1749"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.OctaveCodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generator for Octave code.</p>
<p>The .write() method inherited from CodeGen will output a code file
&lt;prefix&gt;.m.</p>
<p>Octave .m files usually contain one function.  That function name should
match the filename (<code class="docutils literal notranslate"><span class="pre">prefix</span></code>).  If you pass multiple <code class="docutils literal notranslate"><span class="pre">name_expr</span></code> pairs,
the latter ones are presumed to be private functions accessed by the
primary function.</p>
<p>You should only pass inputs to <code class="docutils literal notranslate"><span class="pre">argument_sequence</span></code>: outputs are ordered
according to their order in <code class="docutils literal notranslate"><span class="pre">name_expr</span></code>.</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.OctaveCodeGen.dump_m">
<span class="sig-name descname"><span class="pre">dump_m</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1712-L1742"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.OctaveCodeGen.dump_m" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.OctaveCodeGen.routine">
<span class="sig-name descname"><span class="pre">routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1529-L1617"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.OctaveCodeGen.routine" title="Permalink to this definition">¶</a></dt>
<dd><p>Specialized Routine creation for Octave.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.OutputArgument">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">OutputArgument</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">result_var</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">datatype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dimensions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L395-L438"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.OutputArgument" title="Permalink to this definition">¶</a></dt>
<dd><p>OutputArgument are always initialized in the routine.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.Result">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">Result</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">result_var</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">datatype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dimensions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">precision</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L459-L531"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.Result" title="Permalink to this definition">¶</a></dt>
<dd><p>An expression for a return value.</p>
<p>The name result is used to avoid conflicts with the reserved word
“return” in the python language.  It is also shorter than ReturnValue.</p>
<p>These may or may not need a name in the destination (e.g., “return(x*y)”
might return a value without ever naming it).</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.Routine">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">Routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arguments</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">results</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">local_vars</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L117-L234"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.Routine" title="Permalink to this definition">¶</a></dt>
<dd><p>Generic description of evaluation routine for set of expressions.</p>
<p>A CodeGen class can translate instances of this class into code in a
particular language.  The routine specification covers all the features
present in these languages.  The CodeGen part must raise an exception
when certain features are not present in the target language.  For
example, multiple return values are possible in Python, but not in C or
Fortran.  Another example: Fortran and Python support complex numbers,
while C does not.</p>
<dl class="py property">
<dt class="sig sig-object py" id="sympy.utilities.codegen.Routine.result_variables">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">result_variables</span></span><a class="headerlink" href="#sympy.utilities.codegen.Routine.result_variables" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a list of OutputArgument, InOutArgument and Result.</p>
<p>If return values are present, they are at the end ot the list.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.utilities.codegen.Routine.variables">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">variables</span></span><a class="headerlink" href="#sympy.utilities.codegen.Routine.variables" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a set of all variables possibly used in the routine.</p>
<p>For routines with unnamed return values, the dummies that may or
may not be used will be included in the set.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.codegen.RustCodeGen">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">RustCodeGen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1751-L1965"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.RustCodeGen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generator for Rust code.</p>
<p>The .write() method inherited from CodeGen will output a code file
&lt;prefix&gt;.rs</p>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.RustCodeGen.dump_rs">
<span class="sig-name descname"><span class="pre">dump_rs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routines</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1957-L1958"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.RustCodeGen.dump_rs" title="Permalink to this definition">¶</a></dt>
<dd><p>Write the code by calling language specific methods.</p>
<p>The generated file contains all the definitions of the routines in
low-level code and refers to the header file if appropriate.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>routines</strong> : list</p>
<blockquote>
<div><p>A list of Routine instances.</p>
</div></blockquote>
<p><strong>f</strong> : file-like</p>
<blockquote>
<div><p>Where to write the file.</p>
</div></blockquote>
<p><strong>prefix</strong> : string</p>
<blockquote>
<div><p>The filename prefix, used to refer to the proper header file.
Only the basename of the prefix is used.</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header comment is included on top of each source
file.  [default : True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are included to structure the source
files.  [default : True]</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.RustCodeGen.get_prototype">
<span class="sig-name descname"><span class="pre">get_prototype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">routine</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1862-L1888"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.RustCodeGen.get_prototype" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string for the function prototype of the routine.</p>
<p>If the routine has multiple result objects, an CodeGenError is
raised.</p>
<p>See: <a class="reference external" href="https://en.wikipedia.org/wiki/Function_prototype">https://en.wikipedia.org/wiki/Function_prototype</a></p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.utilities.codegen.RustCodeGen.routine">
<span class="sig-name descname"><span class="pre">routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1765-L1848"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.RustCodeGen.routine" title="Permalink to this definition">¶</a></dt>
<dd><p>Specialized Routine creation for Rust.</p>
</dd></dl>

</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.codegen.codegen">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">codegen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name_expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">language</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">project</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'project'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">to_files</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">standard</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code_gen</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">printer</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L1992-L2144"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.codegen" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate source code for expressions in a given language.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name_expr</strong> : tuple, or list of tuples</p>
<blockquote>
<div><p>A single (name, expression) tuple or a list of (name, expression)
tuples.  Each tuple corresponds to a routine.  If the expression is
an equality (an instance of class Equality) the left hand side is
considered an output argument.  If expression is an iterable, then
the routine will have multiple outputs.</p>
</div></blockquote>
<p><strong>language</strong> : string,</p>
<blockquote>
<div><p>A string that indicates the source code language.  This is case
insensitive.  Currently, ‘C’, ‘F95’ and ‘Octave’ are supported.
‘Octave’ generates code compatible with both Octave and Matlab.</p>
</div></blockquote>
<p><strong>prefix</strong> : string, optional</p>
<blockquote>
<div><p>A prefix for the names of the files that contain the source code.
Language-dependent suffixes will be appended.  If omitted, the name
of the first name_expr tuple is used.</p>
</div></blockquote>
<p><strong>project</strong> : string, optional</p>
<blockquote>
<div><p>A project name, used for making unique preprocessor instructions.
[default: “project”]</p>
</div></blockquote>
<p><strong>to_files</strong> : bool, optional</p>
<blockquote>
<div><p>When True, the code will be written to one or more files with the
given prefix, otherwise strings with the names and contents of
these files are returned. [default: False]</p>
</div></blockquote>
<p><strong>header</strong> : bool, optional</p>
<blockquote>
<div><p>When True, a header is written on top of each source file.
[default: True]</p>
</div></blockquote>
<p><strong>empty</strong> : bool, optional</p>
<blockquote>
<div><p>When True, empty lines are used to structure the code.
[default: True]</p>
</div></blockquote>
<p><strong>argument_sequence</strong> : iterable, optional</p>
<blockquote>
<div><p>Sequence of arguments for the routine in a preferred order.  A
CodeGenError is raised if required arguments are missing.
Redundant arguments are used without warning.  If omitted,
arguments will be ordered alphabetically, but with all input
arguments first, and then output or in-out arguments.</p>
</div></blockquote>
<p><strong>global_vars</strong> : iterable, optional</p>
<blockquote>
<div><p>Sequence of global variables used by the routine.  Variables
listed here will not show up as function arguments.</p>
</div></blockquote>
<p><strong>standard</strong> : string</p>
<p><strong>code_gen</strong> : CodeGen instance</p>
<blockquote>
<div><p>An instance of a CodeGen subclass. Overrides <code class="docutils literal notranslate"><span class="pre">language</span></code>.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">codegen</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">c_name</span><span class="p">,</span> <span class="n">c_code</span><span class="p">),</span> <span class="p">(</span><span class="n">h_name</span><span class="p">,</span> <span class="n">c_header</span><span class="p">)]</span> <span class="o">=</span> <span class="n">codegen</span><span class="p">(</span>
<span class="gp">... </span>    <span class="p">(</span><span class="s2">&quot;f&quot;</span><span class="p">,</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="n">z</span><span class="p">),</span> <span class="s2">&quot;C89&quot;</span><span class="p">,</span> <span class="s2">&quot;test&quot;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_name</span><span class="p">)</span>
<span class="go">test.c</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_code</span><span class="p">)</span>
<span class="go">#include &quot;test.h&quot;</span>
<span class="go">#include &lt;math.h&gt;</span>
<span class="go">double f(double x, double y, double z) {</span>
<span class="go">   double f_result;</span>
<span class="go">   f_result = x + y*z;</span>
<span class="go">   return f_result;</span>
<span class="go">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">h_name</span><span class="p">)</span>
<span class="go">test.h</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_header</span><span class="p">)</span>
<span class="go">#ifndef PROJECT__TEST__H</span>
<span class="go">#define PROJECT__TEST__H</span>
<span class="go">double f(double x, double y, double z);</span>
<span class="go">#endif</span>
</pre></div>
</div>
<p>Another example using Equality objects to give named outputs.  Here the
filename (prefix) is taken from the first (name, expr) pair.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">f</span><span class="p">,</span> <span class="n">g</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Eq</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">c_name</span><span class="p">,</span> <span class="n">c_code</span><span class="p">),</span> <span class="p">(</span><span class="n">h_name</span><span class="p">,</span> <span class="n">c_header</span><span class="p">)]</span> <span class="o">=</span> <span class="n">codegen</span><span class="p">(</span>
<span class="gp">... </span>     <span class="p">[(</span><span class="s2">&quot;myfcn&quot;</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">),</span> <span class="p">(</span><span class="s2">&quot;fcn2&quot;</span><span class="p">,</span> <span class="p">[</span><span class="n">Eq</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">Eq</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">y</span><span class="p">)])],</span>
<span class="gp">... </span>     <span class="s2">&quot;C99&quot;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_name</span><span class="p">)</span>
<span class="go">myfcn.c</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c_code</span><span class="p">)</span>
<span class="go">#include &quot;myfcn.h&quot;</span>
<span class="go">#include &lt;math.h&gt;</span>
<span class="go">double myfcn(double x, double y) {</span>
<span class="go">   double myfcn_result;</span>
<span class="go">   myfcn_result = x + y;</span>
<span class="go">   return myfcn_result;</span>
<span class="go">}</span>
<span class="go">void fcn2(double x, double y, double *f, double *g) {</span>
<span class="go">   (*f) = 2*x;</span>
<span class="go">   (*g) = y;</span>
<span class="go">}</span>
</pre></div>
</div>
<p>If the generated function(s) will be part of a larger project where various
global variables have been defined, the ‘global_vars’ option can be used
to remove the specified variables from the function signature</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">codegen</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">f_name</span><span class="p">,</span> <span class="n">f_code</span><span class="p">),</span> <span class="n">header</span><span class="p">]</span> <span class="o">=</span> <span class="n">codegen</span><span class="p">(</span>
<span class="gp">... </span>    <span class="p">(</span><span class="s2">&quot;f&quot;</span><span class="p">,</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="n">z</span><span class="p">),</span> <span class="s2">&quot;F95&quot;</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">argument_sequence</span><span class="o">=</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">global_vars</span><span class="o">=</span><span class="p">(</span><span class="n">z</span><span class="p">,))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">f_code</span><span class="p">)</span>
<span class="go">REAL*8 function f(x, y)</span>
<span class="go">implicit none</span>
<span class="go">REAL*8, intent(in) :: x</span>
<span class="go">REAL*8, intent(in) :: y</span>
<span class="go">f = x + y*z</span>
<span class="go">end function</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.codegen.get_default_datatype">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">get_default_datatype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">complex_allowed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L260-L282"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.get_default_datatype" title="Permalink to this definition">¶</a></dt>
<dd><p>Derives an appropriate datatype based on the expression.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.codegen.make_routine">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.codegen.</span></span><span class="sig-name descname"><span class="pre">make_routine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">argument_sequence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">global_vars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">language</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'F95'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/codegen.py#L2147-L2233"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.codegen.make_routine" title="Permalink to this definition">¶</a></dt>
<dd><p>A factory that makes an appropriate Routine from an expression.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>name</strong> : string</p>
<blockquote>
<div><p>The name of this routine in the generated code.</p>
</div></blockquote>
<p><strong>expr</strong> : expression or list/tuple of expressions</p>
<blockquote>
<div><p>A SymPy expression that the Routine instance will represent.  If
given a list or tuple of expressions, the routine will be
considered to have multiple return values and/or output arguments.</p>
</div></blockquote>
<p><strong>argument_sequence</strong> : list or tuple, optional</p>
<blockquote>
<div><p>List arguments for the routine in a preferred order.  If omitted,
the results are language dependent, for example, alphabetical order
or in the same order as the given expressions.</p>
</div></blockquote>
<p><strong>global_vars</strong> : iterable, optional</p>
<blockquote>
<div><p>Sequence of global variables used by the routine.  Variables
listed here will not show up as function arguments.</p>
</div></blockquote>
<p><strong>language</strong> : string, optional</p>
<blockquote>
<div><p>Specify a target language.  The Routine itself should be
language-agnostic but the precise way one is created, error
checking, etc depend on the language.  [default: “F95”].</p>
</div></blockquote>
<p><strong>A decision about whether to use output arguments or return values is made</strong></p>
<p><strong>depending on both the language and the particular mathematical expressions.</strong></p>
<p><strong>For an expression of type Equality, the left hand side is typically made</strong></p>
<p><strong>into an OutputArgument (or perhaps an InOutArgument if appropriate).</strong></p>
<p><strong>Otherwise, typically, the calculated expression is made a return values of</strong></p>
<p><strong>the routine.</strong></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="n">make_routine</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">g</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Eq</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">make_routine</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">Eq</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">Eq</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">result_var</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>
<span class="go">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span><span class="p">]</span>
<span class="go">[x, y, f, g]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">result_variables</span><span class="p">]</span>
<span class="go">[f, g]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">local_vars</span>
<span class="go">set()</span>
</pre></div>
</div>
<p>Another more complicated example with a mixture of specified and
automatically-assigned names.  Also has Matrix output.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Matrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">make_routine</span><span class="p">(</span><span class="s1">&#39;fcn&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">,</span> <span class="n">Eq</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">Eq</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="n">g</span><span class="p">),</span> <span class="n">Matrix</span><span class="p">([[</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">result_var</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>  
<span class="go">[result_5397460570204848505]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">results</span><span class="p">]</span>
<span class="go">[x*y]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span><span class="p">]</span>  
<span class="go">[x, y, f, g, out_8598435338387848786]</span>
</pre></div>
</div>
<p>We can examine the various arguments more closely:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.codegen</span> <span class="kn">import</span> <span class="p">(</span><span class="n">InputArgument</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">,</span>
<span class="gp">... </span>                                     <span class="n">InOutArgument</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InputArgument</span><span class="p">)]</span>
<span class="go">[x, y]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">)]</span>  
<span class="go">[f, out_8598435338387848786]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">OutputArgument</span><span class="p">)]</span>
<span class="go">[1, Matrix([[x, 2]])]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InOutArgument</span><span class="p">)]</span>
<span class="go">[g]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">expr</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">arguments</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">InOutArgument</span><span class="p">)]</span>
<span class="go">[g + x]</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Codegen</a><ul>
<li><a class="reference internal" href="#implementation-details">Implementation Details</a></li>
<li><a class="reference internal" href="#routine">Routine</a></li>
<li><a class="reference internal" href="#module-sympy.utilities.codegen">API Reference</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="autowrap.html"
                        title="previous chapter">Autowrap Module</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="decorator.html"
                        title="next chapter">Decorator</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/utilities/codegen.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="decorator.html" title="Decorator"
             >next</a> |</li>
        <li class="right" >
          <a href="autowrap.html" title="Autowrap Module"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Utilities</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Codegen</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/utilities/codegen.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:11 GMT -->
</html>